/*
* 赵晓莉
* */
require(["jquery","template","bootstrap3.min","echarts","bbcare"],function($,template,bootstrap,echarts,$bb,tpl) {
    function watchTrends(dom){
        $(window).resize(function(){
            var width= $(dom).width();
            $(dom+">div").css({"width":width});
            $(dom+">div>canvas").css({"width":width});
        });
    }
    watchTrends(".trends1");
    watchTrends(".trends2");
    watchTrends(".trends3");
    var URL = $bb.baseUrl("department")+"followup/service";

    $bb.ajax({
        url: URL + "/patient/patientStatisticsQuery",
        type: "post",
        contentType: "application/json;charset=UTF-8",
        dataType: "json",
        data: JSON.stringify({
            data: {}
        }),
        success: function (data) {
            var lis=$("#patientInfo").find("li");
            lis.eq(0).find("font").html(data.data.patientTotal);
            lis.eq(1).find("font").html(data.data.activeWeek);
            lis.eq(2).find("font").html(data.data.activeWeekService);
            lis.eq(3).find("font").html(data.data.patientAddWeek);
            $("#allPatients").click(function(){
                $bb.openPage("allPatients");
            })
        },
        error: function (data) {}
    });

    $bb.ajax({
        url: URL + "/servicemanager/patientTotal",
        type: "post",
        contentType: "application/json;charset=UTF-8",
        dataType: "json",
        data: JSON.stringify({
            data: {}
        }),
        success: function (data) {
            var lis=$("#servicePatient").find("li");
            lis.eq(0).find(".patientText").html(data.data.freeCount);
            lis.eq(1).find(".patientText").html(data.data.familymanagerCount);
            lis.eq(2).find(".patientText").html(data.data.singleConsultationCount);
            lis.eq(3).find(".patientText").html(data.data.singleReading);
        },
        error: function (data) {}
    });

    $(".patientTip").find("span").click(function(){
        $(this).addClass("on").siblings().removeClass("on");
    });

    function trend(dom,flag){
        var myChart = echarts.init(document.getElementById(dom));
        var option = {
            title: {
                text: ""
            },
            textStyle: {
                fontSize: "15px"
            },
            tooltip: {
                trigger: 'axis'
            },
            legend: {
                data:['','']
            },
            grid: {
                left: '3%',
                right: '4%',
                bottom: '8%',
                containLabel: true
            },
            toolbox: {
                feature: {
                    saveAsImage: {}
                }
            },
            xAxis: {
                type: 'category',
                boundaryGap: false,
                data: [],
                axisLine:{
                    lineStyle:{
                        color:"#94A3B5"
                    }
                },
                axisLabel:{
                    textStyle:{
                        color:"#94A3B5"
                    }
                }
            },
            yAxis: {
                type: 'value',
                min:0,
                axisLine:{
                    lineStyle:{
                        color:"#94A3B5"
                    }
                },
                axisLabel:{
                    textStyle:{
                        color:"#94A3B5"
                    }
                }
            },
            series: [
                {
                    name:'人数',
                    type:'line',
                    stack: '总量',
                    data:[],
                    color:"#f4511e"
                }
            ]
        };

        $bb.ajax({
            url: URL + "/patient/activeInfo/trendStatistics",
            type: "post",
            contentType: "application/json;charset=UTF-8",
            dataType: "json",
            data: JSON.stringify({
                data:{
                    "flag":flag
                }
            }),
            success: function (data) {
                var x=[];
                var y=[];
                for(var i=0;i<data.data.length;i++){
                    var date=data.data[i].date;
                    x.push(date);
                    y.push(data.data[i].count);
                }
                option.xAxis.data=x;
                option.series[0].data=y;
                myChart.setOption(option);
            },
            error: function (data) {}
        });
    }

    trend("patientOnNum","2");

    $(".patientTip1").find("span").click(function(){
        var index=$(this).index();
        if(index==1){
            trend("patientOnNum","1");
        }else{
            trend("patientOnNum","2");
        }
    });

    $bb.ajax({
        url: URL + "/patient/patientQueueListQuery",
        type: "post",
        contentType: "application/json;charset=UTF-8",
        dataType: "json",
        data: JSON.stringify({
            data: {
                "rows":3
            }
        }),
        success: function (data) {
            var row=data.data.rows;
            var lis="";
            for(var i=0;i<row.length;i++){
                var creatTime=row[i].createTime;
                var d = new Date();
                d.setTime(creatTime);
                var date=d.getMonth()+"月"+d.getDate()+"日";
                var minutes=d.getMinutes();
                (minutes>=10) ? minutes:minutes="0"+minutes;
                var time=d.getHours()+":"+minutes;
                if(row[i].gender=="F"){
                    var sex='<div class="left listLeft"><i class="fa fa-user c-red fs-14" aria-hidden="true"></i></div>';
                }else{
                    sex='<div class="left listLeft"><i class="fa fa-user c-blue fs-14" aria-hidden="true"></i></div>';
                }
                lis+='<li class="clear"><a href="#">'+sex+'\
                           <div class="left listRight">\
                                <p><span>'+row[i].patientName+'</span><span>('+row[i].phoneNum+')</span></p>\
                                <p class="fs-14"><span class="date right">'+date+'&nbsp;&nbsp;'+time+'</span></p>\
                            </div>\
                        </a></li>';
            }
            $("#newPatient").append(lis);
        },
        error: function (data) {}
    });

    function trendVip(dom,flag,type){
        var myChart = echarts.init(document.getElementById(dom));
        var option = {
            title: {
                text: ""
            },
            tooltip: {
                trigger: 'axis'
            },
            legend: {
                data:['','']
            },
            grid: {
                left: '3%',
                right: '5%',
                bottom: '5%',
                containLabel: true
            },
            toolbox: {
                feature: {
                    saveAsImage: {}
                }
            },
            xAxis: {
                type: 'category',
                boundaryGap: false,
                data: [],
                axisLine:{
                    lineStyle:{
                        color:"#94A3B5"
                    }
                },
                axisLabel:{
                    textStyle:{
                        color:"#94A3B5"
                    }
                }
            },
            yAxis: {
                type: 'value',
                min:0,
                axisLine:{
                    lineStyle:{
                        color:"#94A3B5"
                    }
                },
                axisLabel:{
                    textStyle:{
                        color:"#94A3B5"
                    }
                }
            },
            series: [
                {
                    name:'人数',
                    type:'line',
                    stack: '总量',
                    data:[],
                    color:"#f4511e"
                }
            ]
        };
        $bb.ajax({
            url: URL + "/servicemanager/patientTrendStatisics",
            type: "post",
            contentType: "application/json;charset=UTF-8",
            dataType: "json",
            data: JSON.stringify({
                data:{
                    "flag":flag,
                    "type":type
                }
            }),
            success: function (data) {
                var x=[];
                var y=[];
                for(var i=0;i<data.data.length;i++){
                    var date=data.data[i].date;
                    x.push(date);
                    y.push(data.data[i].count);
                }
                option.xAxis.data=x;
                option.series[0].data=y;
                myChart.setOption(option);
            },
            error: function (data) {}
        });
    }

    trendVip("futureChart","2","1");
    trendVip("usingChart","2","2");

    $(".patientTip2").find("span").click(function(){
        var index=$(this).index();
        if(index==1){
            trendVip("futureChart","1","1");
        }else{
            trendVip("futureChart","2","1");
        }
    });

    $(".patientTip3").find("span").click(function(){
        var index=$(this).index();
        if(index==1){
            trendVip("usingChart","1","2");
        }else{
            trendVip("usingChart","2","2");
        }
    });

    $bb.ajax({
        url: URL + "/servicemanager/useStatistics",
        type: "post",
        contentType: "application/json;charset=UTF-8",
        dataType: "json",
        data: JSON.stringify({
            data: {}
        }),
        success: function (data) {
            for(var i=0;i<data.data.length;i++){
                if(data.data[i].serviceType=="1"){
                    $(".onOpen1").html(data.data[i].openServiceNum);
                    $(".onUsing1").html(data.data[i].useServiceNum);
                }else{
                    $(".onOpen2").html(data.data[i].openServiceNum);
                    $(".onUsing2").html(data.data[i].useServiceNum);
                }
            }
        },
        error: function (data) {}
    });

    $bb.ajax({
        url: URL + "/patient/activeInfo/list",
        type: "post",
        contentType: "application/json;charset=UTF-8",
        dataType: "json",
        data: JSON.stringify({
            data: {"rows":5}
        }),
        success: function (data) {
            var lis="";
            for(var i=0;i<data.rows.length;i++){
                lis+='<li class="clear"><a href="#">\
                            <div class="left listVipLeft">\
                                <i class="fa fa-clock-o c-yellow fs-20" aria-hidden="true"></i>\
                                <p class="c-yellow fs-12">'+data.rows[i].opTimeDesc+'</p>\
                            </div>\
                            <div class="left listVipRight">\
                                <p>'+data.rows[i].opDesc+'</p>\
                            </div>\
                        </a></li>'
            }
            $("#officeActive").append(lis);
        },
        error: function (data) {}
    });

    function eduTrends(dom,flag){
        var myChart = echarts.init(document.getElementById(dom));
        var option = {
            title: {
                text: ""
            },
            textStyle: {
                fontSize: "15px"
            },
            tooltip: {
                trigger: 'axis'
            },
            legend: {
                data:['','']
            },
            grid: {
                left: '3%',
                right: '5%',
                bottom: '12%',
                containLabel: true
            },
            toolbox: {
                feature: {
                    saveAsImage: {}
                }
            },
            xAxis: {
                type: 'category',
                boundaryGap: false,
                data: [],
                axisLine:{
                    lineStyle:{
                        color:"#94A3B5"
                    }
                },
                axisLabel:{
                    textStyle:{
                        color:"#94A3B5"
                    }
                }
            },
            yAxis: {
                type: 'value',
                min:0,
                axisLine:{
                    lineStyle:{
                        color:"#94A3B5"
                    }
                },
                axisLabel:{
                    textStyle:{
                        color:"#94A3B5"
                    }
                }
            },
            series: [
                {
                    name:'次数',
                    type:'line',
                    stack: '总量',
                    color:'#f4511e',
                    data:[]
                }
            ]
        };
        $bb.ajax({
            url: URL + "/education/browseStatistics",
            type: "post",
            contentType: "application/json;charset=UTF-8",
            dataType: "json",
            data: JSON.stringify({
                data:{"flag":flag}
            }),
            success: function (data) {
                var x=[];
                var y=[];
                for(var i=0;i<data.data.length;i++){
                    var date=data.data[i].date;
                    x.push(date);
                    y.push(data.data[i].count);
                }
                option.xAxis.data=x;
                option.series[0].data=y;
                myChart.setOption(option);
            },
            error: function (data) {}
        });
    }

    eduTrends("eduChart","2");

    $(".patientTip4").find("span").click(function(){
        var index=$(this).index();
        if(index==1){
            eduTrends("eduChart","1");
        }else{
            eduTrends("eduChart","2");
        }
    });

    $bb.ajax({
        url: URL + "/education/list",
        type: "post",
        contentType: "application/json;charset=UTF-8",
        dataType: "json",
        data: JSON.stringify({
            data: {
                "rows":5,
                "sortField":2
            }
        }),
        success: function (data) {
            var lis="";
            for(var i=0;i<data.rows.length;i++){
                lis+='<li class="clear"><a href="#">\
                            <div class="left listLeft"><span class="cir"></span></div>\
                            <div class="left listRight">\
                                <p>'+data.rows[i].title+'</p>\
                                <p class="right"><i class="fa fa-eye c-yellow" aria-hidden="true"></i>&nbsp;&nbsp;<span class="num">'+data.rows[i].browseCount+'</span></p>\
                            </div>\
                        </a></li>';
            }
            $("#eduList").append(lis);
        },
        error: function (data) {}
    });

    function arcircular(dom,type){
        var myChart = echarts.init(document.getElementById(dom));
        var option = {
            tooltip: {
                trigger: 'item',
                formatter: "{a} <br/>{b}: {c} ({d}%)"
            },
            series: []
        };
        $bb.ajax({
            url: URL + "/patient/monitorDataStatistics",
            type: "post",
            contentType: "application/json;charset=UTF-8",
            dataType: "json",
            data: JSON.stringify({
                data:{
                    "type":type
                }
            }),
            success: function (data) {
                var abnormal=data.data[0].abnormalCount;
                var total=data.data[0].monitorCount;
                var normal=total-abnormal;
                var rate;
                total=="0" ? (rate=0):(rate=parseInt(100*abnormal/total));
                $(".total"+type).html(total);
                $(".proportion"+type).html(rate+"%");
                option.series=[{
                    name:'访问来源',
                    type:'pie',
                    radius: ['50%', '70%'],
                    avoidLabelOverlap: false,
                    label: {
                        normal: {
                            show: false,
                            position: 'center'
                        },
                        emphasis: {
                            show: true,
                            textStyle: {
                                fontSize: '30',
                                fontWeight: 'bold'
                            }
                        }
                    },
                    labelLine: {
                        normal: {
                            show: false
                        }
                    },
                    data:[
                        {value:abnormal, name:'异常'},
                        {value:normal, name:'正常'}
                    ]
                }];
                myChart.setOption(option);
            },
            error: function (data) {}
        });
    }
    arcircular("OChart","1");
    arcircular("PR","2");
    arcircular("temChart","3");

    function trends(dom,flag){
        var myChart = echarts.init(document.getElementById(dom));
        var option = {
            title: {
                text: ''
            },
            textStyle: {
                fontSize: "15px"
            },
            tooltip: {
                trigger: 'axis'
            },
            legend: {
                data:[]
            },
            grid: {
                left: '3%',
                right: '5%',
                bottom: '10%',
                containLabel: true
            },
            toolbox: {
                feature: {
                    saveAsImage: {}
                }
            },
            xAxis: {
                type: 'category',
                boundaryGap: false,
                data: [],
                axisLine:{
                    lineStyle:{
                        color:"#94A3B5"
                    }
                },
                axisLabel:{
                    textStyle:{
                        color:"#94A3B5"
                    }
                }
            },
            yAxis: {
                type: 'value',
                min:0,
                axisLine:{
                    lineStyle:{
                        color:"#94A3B5"
                    }
                },
                axisLabel:{
                    textStyle:{
                        color:"#94A3B5"
                    }
                }
            },
            series: []
        };
        $bb.ajax({
            url: URL + "/patient/monitorTimesStatistics",
            type: "post",
            contentType: "application/json;charset=UTF-8",
            dataType: "json",
            data: JSON.stringify({
                data:{
                    "flag":flag,
                    "type":"2"
                }
            }),
            success: function (data) {
                var x=[];
                var y=[];
                for(var i=0;i<data.data.length;i++){
                    var date=data.data[i].date;
                    x.push(date);
                    y.push(data.data[i].count);
                }
                option.xAxis.data=x;
                var os={
                    name:'温度',
                    type:'line',
                    data:y,
                    itemStyle : {
                        normal : {
                            color:'#26a69a',
                            lineStyle:{
                                color:'#26a69a'
                            }
                        }
                    }
                };
                option.series.push(os);
                $bb.ajax({
                    url: URL + "/patient/monitorTimesStatistics",
                    type: "post",
                    contentType: "application/json;charset=UTF-8",
                    dataType: "json",
                    data: JSON.stringify({
                        data:{
                            "flag":flag,
                            "type":"1"
                        }
                    }),
                    success: function (data) {
                        var yTem=[];
                        for(var i=0;i<data.data.length;i++){
                            yTem.push(data.data[i].count);
                        }
                        var obj={
                                name:'氧饱脉率',
                                type:'line',
                                data:yTem,
                                itemStyle : {
                                    normal : {
                                        color:'#ff7043',
                                        lineStyle:{
                                            color:'#ff7043'
                                        }
                                    }
                                }
                            };
                        option.series.push(obj);
                        myChart.setOption(option);
                    },
                    error: function (data){}
                });
            },
            error: function (data) {}
        });
    }
    trends("OTemTrends","2");

    $(".patientTip5").find("span").click(function(){
        var index=$(this).index();
        if(index==1){
            trends("OTemTrends","1");
        }else{
            trends("OTemTrends","2");
        }
    });

    $bb.ajax({
        url: URL + "/patient/activeInfo/list",
        type: "post",
        contentType: "application/json;charset=UTF-8",
        dataType: "json",
        data: JSON.stringify({
            data: {
                "busiCategory":"6",
                "rows":"10"
            }
        }),
        success: function (data) {
            var lis="";
            var lis2='';
            for(var i=0;i<data.rows.length;i++){
                lis+='<li class="clear"><a href="#">\
                        <div class="left listLeft"><span class="cir"></span></div>\
                        <div class="left listRight">\
                            <p>'+data.rows[i].opDesc+'<span class="right fs-12">'+data.rows[i].opTimeDesc+'</span></p>\
                        </div>\
                    </a></li>'
            }
            var len=10-data.rows.length;
            for(var i=0;i<len;i++){
                lis+='<li class="clear"><a href="#"></a></li>'
            }
            $("#patientWatchInfo").append(lis).append(lis2);
        },
        error: function (data) {}
    });
});